﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using xTeam.Glgcx.DataAccess;
using xTeam.Glgcx.CommonUtilities;
using ShopB2C.Core.Helpers;

namespace xTeam.Glgcx.Business
{
    public class ArchiveBal
    {
        private ArchiveDataAccess _archiveDataAccess;
        private ClassifyDataAccess _classifyDataAccess;

        public ArchiveBal()
        {
            _archiveDataAccess = new ArchiveDataAccess();
            _classifyDataAccess = new ClassifyDataAccess();
        }
        #region 基础信息
        public ENews GetArchiveById(int id, bool? isValid)
        {

            var archive = _archiveDataAccess.GetArchiveById(id);
            if (isValid != null && archive.Isvalid != isValid)
                archive = null;
            return archive;
        }

        public List<ENews> ArchiveList(string key, int pageIndex, int pageSize, int classiyfId, bool? isValid, ref int totalRows)
        {



            var datasource = _archiveDataAccess.ArchiveList()
                .Where(i => (isValid == null || i.Isvalid == isValid)
                    && (string.IsNullOrEmpty(key) || i.Title.Contains(key) && i.Keywords.Contains(key) && i.Description.Contains(key) && i.Contents.Contains(key))
                    )
                    .OrderByDescending(i => i.AddDateTime).ThenBy(i => i.Sequence);

            totalRows = datasource.Count();
            return datasource.Skip((pageIndex - 1) * pageSize).Take(pageSize)
                    .ToList(); ;
        }


        public int Update(ENews news)
        {
            if (string.IsNullOrEmpty(news.ETag))
                news.ETag = news.Title;
            return _archiveDataAccess.Update(news);
        }

        public int Delete(int newsId)
        {
            return _archiveDataAccess.Delete(newsId);
        }

        public ArchiveViewModel GetArchiveListByClassifyETag(string eTag, int pageSize, int imageCount, ETagType eTagType)
        {
            ArchiveViewModel viewModel = new ArchiveViewModel();
            List<ENews> archiveList = new List<ENews>();
            EClassify classify;
            if (eTagType == ETagType.Hanzi)
            {
                classify = _classifyDataAccess.GetClassifyByTagName(eTag);
            }
            else
            {
                classify = _classifyDataAccess.GetClassifyByPinYinTagName(eTag);

            }
            if (classify != null)
            {
                archiveList = _archiveDataAccess.ArchiveList()
                    .Join(classify.Descendant, a => a.ClassifyId, b => b.ClassifyId, (a, b) => a)
                    .Where(i => i.Album.Count >= imageCount)
                    .OrderByDescending(i => i.AddDateTime)
                    .Take(pageSize)
                    .ToList();

            }
            classify = classify ?? new EClassify() { ETag = eTag, Title = eTag };
            viewModel.Classify = classify;
            viewModel.ArchiveList = archiveList;
            viewModel.PageSize = pageSize;

            return viewModel;
        }
        public ArchiveViewModel GetArchiveListByClassifyETag(string eTag, int pageSize, ETagType eTagType)
        {
            return this.GetArchiveListByClassifyETag(eTag, pageSize, 0, eTagType);
        }
        public ArchiveViewModel GetArchiveListByClassifyETag(string eTag, int pageSize, int imageCount)
        {
            return this.GetArchiveListByClassifyETag(eTag, pageSize, imageCount, ETagType.Hanzi);
        }
        public ArchiveViewModel GetArchiveListByClassifyETag(string eTag, int pageSize)
        {
            return this.GetArchiveListByClassifyETag(eTag, pageSize, 0);
        }
        public ENews GetArchiveByETag(string tagName)
        {
            return _archiveDataAccess.ArchiveList().FirstOrDefault(i => ChineseCharHelper.ConvertToPinYin(i.ETag, "-") == tagName);
        }


        #endregion
        #region 图片信息
        public int SaveImage(EAlbum eAlbum)
        {
            return _archiveDataAccess.UpdateAlbum(eAlbum);
        }

        public int SetCover(int imageId)
        {
            return _archiveDataAccess.SetCover(imageId);
        }

        public List<EAlbum> DeleteImage(string[] ids)
        {
            return _archiveDataAccess.DeleteAlbum(ids);

        }
        public void SaveImage(int imageId, string title, string desc, int sequence)
        {
            _archiveDataAccess.UpdateAlbum(imageId, title, desc, sequence);
        }
        #endregion











    }
}
